package com.cy.http;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpResponse {
    private Socket socket;
    private OutputStream out;

    //状态行相关信息
    private int statusCode = 200;
    private String statusReason = "OK";
    //响应头相关信息
    private Map<String ,String > headers = new HashMap<>();
    //响应正文相关信息
    private File entity;
    private byte[] contentData;

    public HttpResponse(Socket socket) {
        try {
            this.socket = socket;
            this.out = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void flush(){
        //发送状态行
        sendStatusLine();
        //发送响应头
        sendHeaders();
        //发送相应正文
        sendContent();
    }

    private void sendStatusLine() {
        String line = "HTTP/1.1 "+statusCode+" "+statusReason;
        println(line);
    }

    private void sendHeaders() {
        Set<Map.Entry<String, String>> entrySet = headers.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            String line = entry.getKey()+": "+entry.getValue();
            println(line);
        }
        println("");
    }

    private void sendContent() {
        if(contentData != null) {
            try {
                out.write(contentData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(entity!=null){
            try(FileInputStream fis = new FileInputStream(entity)){
                byte[] data = new byte[1024*10];
                int len = -1;
                while ((len = fis.read(data))!=-1){
                    out.write(data,0,len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendRedirect(String url) {
        //1设置对应的状态代码
        setStatusCode(302);
        //2设置对应的响应头
        headers.put("Location", url);
    }

    public byte[] getContentData() {
        return contentData;
    }

    public void setContentData(byte[] contentData) {
        this.contentData = contentData;
    }

    public void putHeader(String name,String value) {
        this.headers.put(name, value);
    }

    public String getHeader(String key){
        return headers.get(key);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
        this.statusReason = HttpContext.getStatusReason(statusCode);
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {
        this.entity = entity;
        String fileName= entity.getName();
        //根据文件名的后缀，获取对应的mime类型
        String ext = fileName.substring(fileName.lastIndexOf(".")+1);
        String type = HttpContext.getMimeType(ext);
        headers.put("Content-Type", type);
        headers.put("Content-Length", entity.length()+"");
    }

    private void println(String line){
        try {
            out.write(line.getBytes(StandardCharsets.ISO_8859_1));
            out.write(13);
            out.write(10);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
